package com.pillar.enty;

import lombok.Data;
import net.bytebuddy.utility.RandomString;

import java.io.Serializable;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author pillar
 * @desc  游戏大厅
 * @email: 347637454@qq.com
 * @createDate 2021-01-08 21:15
 */
@Data
public final class Hall implements Serializable {

    private final int MAX_ROOM_NUM = 100;

    private final List<Room> roomList = new CopyOnWriteArrayList<>();

    public boolean fullRoom(){
        return MAX_ROOM_NUM == roomList.size();
    }


    public Room createRoom(String uuid) {
        Room room = new Room();
        room.setRoomId(UUID.randomUUID().toString());
        room.setRoomName("高级VIP房间");
        Player p = new Player();
        p.setId(RandomString.make());
        p.setUuid(uuid);
        p.setNickname("李元芳");
        p.setState("IN");
        room.join(p);
        roomList.add(room);
        return room;
    }

    /**
     * TODO 加入房间以后返回进入已准备的所有玩家信息
     * @param uuid
     * @param room
     */
    public void join(String uuid, Room room) {
        Player p = new Player();
        p.setState("IN");
        p.setNickname("皮蛋玩家");
        p.setUuid(uuid);
        p.setId(RandomString.make());
        room.join(p);
    }

    public Room findRoom(String roomId) {
        return roomList.stream().filter(room -> room.getRoomId().equals(roomId)).findFirst().orElse(null);
    }

    public Room findRoomByUuid(String uuid) {
        return roomList.stream().filter(room -> room.getPlayerList().stream().anyMatch(player -> player.getUuid().equals(uuid))).findFirst().orElse(null);
    }
}
